<?php

namespace app\user\event\subscribes\user;

use app\user\event\listeners\user\Credit;
use app\user\event\listeners\user\RegNotify;
use app\user\event\listeners\user\SendPwdSetSms;
use app\user\event\listeners\user\UserLastLogin;
use app\user\event\listeners\user\UserLoginRecord;
use app\user\event\listeners\user\UserTokenRefresh;
use app\user\event\listeners\user\UserTicket;
use app\user\logic\teacher\Teacher;
use think\Event;

class Account
{
    public function subscribe(Event $event)
    {
        /** UserLogin 登录成功 */
        $event->listen('UserLogin', UserLastLogin::class);
        $event->listen('UserLogin', UserTokenRefresh::class);
        $event->listen('UserLogin', UserLoginRecord::class);
        $event->listen('UserLogin', UserTicket::class);

        /** UserLogin 注册成功 */
        $event->listen('UserReg', UserLastLogin::class);
        $event->listen('UserReg', UserTokenRefresh::class);
        $event->listen('UserReg', UserLoginRecord::class);
        $event->listen('UserReg', Credit::class);
        $event->listen('UserReg', RegNotify::class);
        $event->listen('UserReg', UserTicket::class);

        /** 用户账号创建成功 */
        $event->listen('UserCreated', Credit::class);

        /* UserPwdSet 设置密码 */
        $event->listen('UserPwdSet', SendPwdSetSms::class);

        /* UserPwdSet 重置密码 */
        $event->listen('UserPwdReset', SendPwdSetSms::class);
        /* UserAutoLogin 自动登录*/
        $event->listen('UserAutoLogin', UserLastLogin::class);

        // 如果是APP端口的自动登录,则需要记录登录记录
        if (in_array(request()->device_type, ['ios', 'android'])) {
            $event->listen('UserAutoLogin', UserLoginRecord::class);
        }

        /** 更新 */
        $event->listen('UserUpdated', function ($user) {
            $teacher = app(Teacher::class)->where('user_id', $user->id)->findOrEmpty();
            if (!$teacher->isEmpty()) {
                cache('recommend_teacher', null);
            }
        });

    }
}
